'use strict';

module.exports = () => {
	return async function(ctx, next){
		let token = '';
		if(ctx.headers.authorization && ctx.headers.authorization.split(' ')[0] === 'Bearer'){
			token = ctx.headers.authorization.split(' ')[1];
		}

		const user = await ctx.service.user.getUserByToken(token);
		if(!user){
			ctx.staus = 401;
			ctx.body = {
				code: 'SUCCESS',
				msg: '错误的激活码',
			};
			return;
		}

		ctx.request.user = user;
		await next();
	}
}